package com.jiandaoshou;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.fragments.DingDanFragment;
import com.jiandaoshou.fragments.ShouYeFragment;
import com.jiandaoshou.fragments.ZhanghuFragment;
import com.jiandaoshou.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<String> titles;
    private List<Fragment> fragments;
    private MyPagerAdapter adapter;
    private List<Integer> IconImg;
    //商家的ID
    private String shopID;
    @Override
    protected int getContentViewRes() {
        return R.layout.activity_main;
    }

    @Override
    protected String getTitleText() {
        return "剪刀手";
    }

    @Override
    public boolean showBackLogo() {
        return false;
    }

    @Override
    public void init() {
        mViewPager = (ViewPager) $(R.id.viewpager);
        mTabLayout = (TabLayout) $(R.id.tablayout);
        //获取shopID
        shopID= UserUtils.getUserid(context);
        titles = new ArrayList<String>();
        titles.add("首页");
        titles.add("订单");
        titles.add("账户");
        IconImg = new ArrayList<Integer>();
        IconImg.add(R.drawable.selector_tab_shouye);
        IconImg.add(R.drawable.selector_tab_dingdan);
        IconImg.add(R.drawable.selector_tab_zhanghu);
        fragments = new ArrayList<Fragment>();
        //给首页Fragment传入参数
        Bundle bundle=new Bundle();
        bundle.putString("shopId",shopID);
        Fragment ShouYeFragment= new ShouYeFragment();
        ShouYeFragment.setArguments(bundle);
        fragments.add(ShouYeFragment);
        DingDanFragment DingDanfragment=new DingDanFragment();
        DingDanfragment.setArguments(bundle);
        fragments.add(DingDanfragment);
        ZhanghuFragment zhanghufragment=new ZhanghuFragment();
        zhanghufragment.setArguments(bundle);
        fragments.add(zhanghufragment);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        adapter = new MyPagerAdapter(this.getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setCustomView(adapter.getTabView(0));
        mTabLayout.getTabAt(1).setCustomView(adapter.getTabView(1));
        mTabLayout.getTabAt(2).setCustomView(adapter.getTabView(2));
        TextView textView = (TextView) mTabLayout.getTabAt(0).getCustomView();
        textView.setTextColor(getResources().getColor(R.color.theme_color));
        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_shouye_selected, 0, 0);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());//点击哪个就跳转哪个界面
                TextView textView = (TextView) tab.getCustomView();
                textView.setTextColor(getResources().getColor(R.color.theme_color));
                switch (tab.getPosition()) {
                    case 0:
                        toolbarTitleTextView.setText("首页");
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_shouye_selected, 0, 0);
                        menu.findItem(R.id.shaixuan).setVisible(false);
                        menu.findItem(R.id.mingxi).setVisible(false);
                        break;
                    case 1:
                        toolbarTitleTextView.setText("订单");
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_dingdan_selected, 0, 0);
                        menu.findItem(R.id.shaixuan).setVisible(true);
                        menu.findItem(R.id.mingxi).setVisible(false);
                        break;
                    case 2:
                        toolbarTitleTextView.setText("账户");
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_zhanghu_selected, 0, 0);
                        menu.findItem(R.id.shaixuan).setVisible(false);
                        menu.findItem(R.id.mingxi).setVisible(true);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView textView = (TextView) tab.getCustomView();
                textView.setTextColor(Color.BLACK);
                switch (tab.getPosition()) {
                    case 0:
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_shouye_unselected, 0, 0);
                        break;
                    case 1:
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_dingdan_unselected, 0, 0);
                        break;
                    case 2:
                        textView.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.icon_zhanghu_unselected, 0, 0);
                        break;
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        public TextView getTabView(int position) {
            View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_tab, null);
            TextView rbt = (TextView) v;
            rbt.setText(titles.get(position));
            rbt.setCompoundDrawablesWithIntrinsicBounds(0, IconImg.get(position), 0, 0);
            return rbt;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

    private Menu menu;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_shaixuan, menu);
        this.menu = menu;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent=null;
        switch (item.getItemId()) {
            case R.id.shaixuan:
                intent=new Intent(this, DingDanShaiXuanAct.class);
                intent.putExtra("shopId",shopID);
                startActivity(intent);
                break;
            case R.id.mingxi:
                intent=new Intent(this, ZhanghuDeTailAct.class);
                intent.putExtra("shopId",shopID);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

